#!/bin/sh
set -xe

ADMIN=$(dirname "$0")
PREV=$(pwd)
if [ ! -d $ADMIN/rustfmt ] ; then
  COMMIT=8829e455b722f1ba8ade915ad2687639a2abb2c3
  cd $ADMIN
  git init rustfmt
  cd rustfmt
  git fetch --depth=1 https://github.com/rust-lang/rustfmt $COMMIT
  git checkout $COMMIT
  cd ../..
fi

if [ ! -x $ADMIN/rustfmt/target/release/cargo-fmt ] ; then
  cd $ADMIN/rustfmt
  export CFG_RELEASE=nightly
  export CFG_RELEASE_CHANNEL=nightly
  cargo build --release --features rustfmt,cargo-fmt
fi

cd $PREV
PATH=$ADMIN/rustfmt/target/release/:$PATH cargo-fmt "$@"
